<?php

namespace Index\Controller;

use Think\Controller;
use Index\ViewModel;
use Index\Service;

/**
 * 说明：消息管理控制器
 * 作者：王楠
 * 创建日期：2017-09-15
 */
class MessageController extends CommonController
{

    public function messagelist()
    {
        $app_code = $this->app_code;
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $appcode = trim(I('ddlapp'));

        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            if (!empty($appcode)) {
                $map['app_flag'] = $appcode;
            }
        }
        if (!empty($state)) {
            $map['state'] = $state;
        }
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            $where['chat_message.id'] = array('eq', $keyword);
            $where['chat_message.content'] = array('like', '%' . $keyword . '%');
            $where['chat_admin.username'] = array('like', '%' . $keyword . '%');
            $where['chat_admin.name'] = array('like', '%' . $keyword . '%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $model = new ViewModel\MessageViewModel();
        $total = $model->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();
        $data = $model->where($map)->limit($page->listfirst, $page->listRows)->order('ctime desc')->select();
        foreach ($data as $key => $value) {
            $data[$key]['admin_name'] = empty($data[$key]['admin_name']) ? $data[$key]['username'] : $data[$key]['admin_name'];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
        }
        $search = array('appsel' => $appcode, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'search' => $search,
            'app_code' => $app_code,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }

    public function messageaddsubmit()
    {
        $app_code = $this->app_code;
        $admin_id = $this->admin_id;
        $content = I('content');
        $data = array(
            'admin_id' => $admin_id,
            'content' => $content,
            'ctime' => time(),
            'app_flag' => $app_code,
        );
        $model = M('chat_message');
        $result = $model->add($data);
        if ($result) {
            $response = array('ret' => '1001', 'code' => '添加成功！');
        } else {
            $response = array('ret' => '1002', 'code' => '添加失败！');
        }
        return $this->ajaxReturn($response, 'json');
    }

    public function deletesubmit()
    {
        $id = I('id');
        if (!empty($id)) {
            $model = M('chat_message');
            $result = $model->where(array('id' => $id))->delete();
            if ($result) {
                return $this->ajaxReturn(array('ret' => '1001', 'code' => '删除成功！'));
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '删除失败！'));
            }
        }
    }

}
